(function(angular) {
	'use strict';
	angular.module("fs.waf").factory("docTemplateService", docTemplateService);
	docTemplateService.$inject = [ '$q', 'loggerFactory', 'modalService', 'gridService','ajaxServiceFactory' ,'storageService', '$filter'];
	function docTemplateService($q, loggerFactory, modalService, gridService, ajaxServiceFactory, storageService, $filter) {
		var log = loggerFactory.getLogger("docTemplateService");
		var ajaxService = ajaxServiceFactory.buildAjaxService({
			//docEngine
			getDocEngine : {
				url : 'do/cda/dataList/docEngine',
				method : 'get'
			},
			getDataSource : {
				url : 'do/cda/dataList/dataSource',
				method : 'get'
			},
			getServiceDoctype : {
				url : 'do/cda/dataList/serviceDoctype',
				method : 'get'
			},
			getService : {
				url : 'do/cda/dataList/serviceCode',
				method : 'get'
			},
			getDataStatus : {
				url : 'do/cda/dataList/dataStatus',
				method : 'get'
			},
			getDocNameParms : {
				url : 'do/cda/dataList/docNameParms',
				method : 'get'
			},
			getLocaleCodeOptions :{
				url : 'do/cda/dataList/getLocaleCodeOptions',
				method : 'get'
			}
		});
		
		function getLocaleCodes() {
			var defer = $q.defer();
			ajaxService.getLocaleCodeOptions().then(function(response) {
				defer.resolve(response);
			});
			return defer.promise;
		}
		function getDocNameParms() {
			var defer = $q.defer();
			ajaxService.getDocNameParms().then(function(response) {
				defer.resolve(response);
			});
			return defer.promise;
		}
		function getDataStatus() {
			var defer = $q.defer();
			ajaxService.getDataStatus().then(function(response) {
				defer.resolve(response);
			});
			return defer.promise;
		}
		function getServiceOptions() {
			var defer = $q.defer();
			ajaxService.getService().then(function(response) {
				defer.resolve(response);
			});
			return defer.promise;
		}

		function getDocEngineOptions() {
			var defer = $q.defer();
			ajaxService.getDocEngine().then(function(response) {
				defer.resolve(response.data);
			});
			return defer.promise;
		}
		
		function getDataSourceOptions() {
			var defer = $q.defer();
			ajaxService.getDataSource().then(function(response) {
				defer.resolve(response);
			});
			return defer.promise;
		}
		function getServiceDoctypeOptions() {
			var defer = $q.defer();
			ajaxService.getServiceDoctype().then(function(response) {
				defer.resolve(response);
			});
			return defer.promise;
		}

		return {
			getLocaleCodes : getLocaleCodes,
			getDataStatus : getDataStatus,
			getDocEngineOptions : getDocEngineOptions,
			getDataSourceOptions : getDataSourceOptions,
			getServiceDoctypeOptions : getServiceDoctypeOptions,
			getDataSourceOptions : getDataSourceOptions,
			initDataOptions : initDataOptions,
			initParmOptions : initParmOptions,
			setPrePostParm : setPrePostParm,
			setDocPrePoParm : setDocPrePoParm,
			getServiceOptions : getServiceOptions
		};
		
		function initDataOptions($scope, ctrl) {			
			getDocNameParms().then(function(response) {
				ctrl.docNameParms = ctrl.docNameParms ||{};
				ctrl.docNameParms.data = [];
				var s = [];
				if (response && response.data) {
					angular.forEach(response.data, function(data,
							index, array) {
						if (s.indexOf(data.value) === -1) {
							s.push(data.value);
						}
					});
					if (s.length > 0) {
						for (var i = 0; i < s.length; i++) {
							ctrl.docNameParms.data.push({
								"value" : '{'+s[i]+'}',
								"label" : s[i]
							});
						}
					}
				}
			});
			
			getServiceOptions().then(function(response) {
				ctrl.serviceCodeOptions = response.data;
			});
			
			getDataStatus().then(function(response) {
				ctrl.dataStatusOptions = response.data;
			});
			
			//get all serviceDocType options 
			getServiceDoctypeOptions().then(function(response){
				ctrl.docTypeOptions = [];
				ctrl.allDocTypeOptions = response.data;
			});
			
			//change serviceDocType options by serviceCode
			ctrl.getDocTypesByService = function (serviceValue) {
				ctrl.docTemplate.serviceCode = ctrl.docTemplate.serviceCode;
				ctrl.docTemplate.siteCode = storageService.getUserInfo().siteCode;
				if (serviceValue.serviceCode != null ){
					ctrl.getServiceDoctypeOptionsByService(serviceValue);
				}
			};	
			ctrl.getServiceDoctypeOptionsByService = function(serviceValue){
				ctrl.docTypeOptions.data = [];
				if ( serviceValue != null){
					serviceValue.serviceCode = $filter('encode')(serviceValue.serviceCode);
				}
				var options;
				var labelObj = [];
				angular.forEach(ctrl.allDocTypeOptions, function(data, index, array){
					if(data.value.indexOf(serviceValue.serviceCode) != -1 ){
						labelObj.push(data);
					} 
				});
				ctrl.docTypeOptions.data = labelObj;
			};
			
			getDocEngineOptions().then(function (options){		
				ctrl.docEngineCodeOptions = [];
				ctrl.docFormatsOptions = [];
				angular.forEach(options, function(value, key){
					ctrl.docEngineCodeOptions.push({
						"label" : value.extProperty + " - " + value.value,
						"value" : value.extProperty,
						"extProperty" : value.label,
						"format" : value.value
					});
					ctrl.docFormatsOptions.push({
						"label" : value.value,
						"value" : value.value,
						"format" : value.value
					});
				});
			});
			
			//select docEngineId, set docEngineCode
			ctrl.setDocEngineId = function(value) {
				ctrl.docTemplate.dgeEngineId = ctrl.changeDocEngineId(ctrl.docTemplate.dgeEngineCode);
				ctrl.docTemplate.docFormat = ctrl.setDocFormat(ctrl.docTemplate.dgeEngineCode);
			};
			
			ctrl.setDocFormat = function(code){
				var options;
				var labelObj = [];
				angular.forEach(ctrl.docEngineCodeOptions, function(data, index, array){
					if(data.value == code){
						ctrl.docTemplate.docFormat = data.format;
					} 
				});
				return ctrl.docTemplate.docFormat;
			};
			ctrl.changeDocEngineId = function(code){
				var options;
				var labelObj = [];
				angular.forEach(ctrl.docEngineCodeOptions, function(data, index, array){
					if(data.value == code){
						ctrl.docTemplate.dgeEngineId = data.extProperty;
					} 
				});
				return ctrl.docTemplate.dgeEngineId;
			};
			getDataSourceOptions().then(function(response){
				ctrl.datasourceOptions = response;
				ctrl.datasourceOptions.hasEmptyOption = true;
			});
			
			//trigger locale level after set localCode
			ctrl.setLocalLevel = function (){
				if (!ctrl.docTemplate.localeCodes || ctrl.docTemplate.localeCodes == ",,"){
					ctrl.docTemplate.localeLevel = 7;
				} else {
					ctrl.docTemplate.localeLevel = 3;
				}
			};
		}
		
		//get value from ctrl.docTemplate.docTempPreParms/docTempPostParms to ctrl.docTemplate.preprocParms/postprocParms(String)
		function setPrePostParm($scope, ctrl) {
			var preParm = [], preParms = [];
			if (ctrl.docTemplate.docTempPreParms && ctrl.docTemplate.docTempPreParms.length >0) {
				angular.forEach(ctrl.docTemplate.docTempPreParms, function (value, key){
					//value: {name: "S", type: "java.lang.String", desc: "S", value: "S"}
					preParm = angular.toJson(value);
					preParms.push(preParm);
					ctrl.docTemplate.preprocParms = preParms.toString();
				});
			}
			var postParm = [], postParms = [];
			if (ctrl.docTemplate.docTempPostParms && ctrl.docTemplate.docTempPostParms.length >0) {
				angular.forEach(ctrl.docTemplate.docTempPostParms, function (value, key){
					postParm = angular.toJson(value);
					postParms.push(postParm);
					ctrl.docTemplate.postprocParms = postParms.toString();
				});
			}
		}
		
		// set List<VoDocTempParam> docTempPreParms from ctrl.docTemplate.preprocParms/postprocParms(String) 
		function setDocPrePoParm($scope, ctrl) {
			// "{name: "S", type: "java.lang.String", desc: "S", value: "S"}"
			ctrl.docTemplate.preprocParms = ctrl.docTemplate.preprocParms;
			ctrl.docTemplate.postprocParms = ctrl.docTemplate.postprocParms;
			
			ctrl.docTemplate.docTempPreParms = [];
			ctrl.docTemplate.docTempPostParms = [];
			//set parms from string parms(preprocParms,postprocParms)
			if (ctrl.docTemplate.preprocParms != null ) {	
				if (ctrl.docTemplate.preprocParms.split("},{").length > 0) {
					//"{"name":"21","type":"java.lang.String","desc":"21","value":"21"},{"name":"22","type":"java.lang.String","desc":"22","value":"22"}"
					 var a = ctrl.docTemplate.preprocParms.replace(/},{/g, "},,{");
					//"{"name":"21","type":"java.lang.String","desc":"21","value":"21"},,{"name":"22","type":"java.lang.String","desc":"22","value":"22"}"
					var b = a.split(",,");
					angular.forEach(b, function (value,key){
						ctrl.docTemplate.docTempPreParms.push(angular.fromJson(angular.fromJson(angular.toJson(value))));
					});
				} else {
					ctrl.docTemplate.docTempPreParms.push(angular.fromJson(angular.fromJson(angular.toJson(ctrl.docTemplate.preprocParms))));
				}
			}
			if (ctrl.docTemplate.postprocParms != null) {		
				if (ctrl.docTemplate.postprocParms.split("},{").length > 0) {
					//"{"name":"21","type":"java.lang.String","desc":"21","value":"21"}, {"name":"22","type":"java.lang.String","desc":"22","value":"22"}"
					 var a = ctrl.docTemplate.postprocParms.replace(/},{/g, "},,{");
					//"{"name":"21","type":"java.lang.String","desc":"21","value":"21"},, {"name":"22","type":"java.lang.String","desc":"22","value":"22"}"
					var b = a.split(",,");
					angular.forEach(b, function (value,key){
						ctrl.docTemplate.docTempPostParms.push(angular.fromJson(angular.fromJson(angular.toJson(value))));
					});
				} else {
					ctrl.docTemplate.docTempPostParms.push(angular.fromJson(angular.fromJson(angular.toJson(ctrl.docTemplate.postprocParms))));
				}
			}
		}
		
		//init dTempParms
		function initParmOptions($scope, ctrl) {
			ctrl.innerPreGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.docTempPreParms.tableConfigs.table, rowSelectionChanged);
			ctrl.innerPreGridOptions.data = 'vm.docTemplate.docTempPreParms';
			ctrl.innerPreGridOptions.verifyData = 'vm.docTemplateOld.docTempPreParms';
			
			ctrl.innerPostGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.docTempPostParms.tableConfigs.table, rowSelectionChanged);
			ctrl.innerPostGridOptions.data = 'vm.docTemplate.docTempPostParms';
			ctrl.innerPostGridOptions.verifyData = 'vm.docTemplateOld.docTempPostParms';
			
			function rowSelectionChanged(eventRow, selectedRows) {
				ctrl.selectedDTParmList = selectedRows;
			}
			
		}
		
	}
})(angular);